//
//  YSMenuTopView.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSMenuTopView.h"
#import "Masonry.h"

@interface YSMenuTopView ()

@property (nonatomic, weak) UIButton *changeSourceButton;
@property (nonatomic, weak) UIButton *reloadButton;


@end

@implementation YSMenuTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setIsLocal:(BOOL)isLocal {
    self.changeSourceButton.hidden = isLocal;
    self.reloadButton.hidden = isLocal;
}

- (void)setupSubViews {

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [backButton setImage:[[UIImage imageNamed:@"G_Back_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];

    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(50, 44));
    }];

    UIButton *changeSourceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.changeSourceButton = changeSourceButton;
    [changeSourceButton setTitle:@"换源" forState:UIControlStateNormal];
    changeSourceButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [changeSourceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [changeSourceButton addTarget:self action:@selector(changeSourceButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:changeSourceButton];

    UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.reloadButton = reloadButton;
    [reloadButton setTitle:@"刷新" forState:UIControlStateNormal];
    reloadButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [reloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [reloadButton addTarget:self action:@selector(reloadButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:reloadButton];

    [reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(50, 44));
    }];

    [changeSourceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(reloadButton.mas_left).offset(-10);
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(50, 44));
    }];
}

- (void)backButtonDidClick:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuTopView:didClickBackButton:)]) {
        [self.delegate menuTopView:self didClickBackButton:btn];
    }
}

- (void)changeSourceButtonDidClick:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuTopView:didClickChangeSourceButton:)]) {
        [self.delegate menuTopView:self didClickChangeSourceButton:btn];
    }
}

- (void)reloadButtonDidClick:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuTopView:didClickReloadButton:)]) {
        [self.delegate menuTopView:self didClickReloadButton:btn];
    }
}

@end
